#define pwmVer	1
#define pwmRev	0

/****************************************************************************
*
*   Copyright (c) 2008 Matt Bateman
*   Robotics4Fun http://www.laughingsky.com/hobbies/robotics/
*   (please link back if you use this code!)
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License version 2 as
*   published by the Free Software Foundation.
*
*   Alternatively, this software may be distributed under the terms of BSD
*   license.
*
****************************************************************************/

/****************************************************************************
*   Version Notes
*	
*   1.0   10/21/2008  Initial release
****************************************************************************/

//#define pwmDebug 			1				// Turn on debug output
#define pwmShowInitStatus	1				// Display init status and version info

void pwmInitE345(void) {
	/***************************************************/
	/* Initialize timer for PWM use on pins E3, E4, E5 */
	/***************************************************/
	// 00	A off, toggle on compare
	// 00	B on, toggle on compare
	// 00	C on, toggle on compare
	// 00	Low part of WGM: PWM, phase & freq, ICRn
	TCCR3A=0x00;
	// 0	No input noise cancelling
	// 0	Input edge select does not matter
	// 0	Not used
	// 10	High part of WGM: PWM, phase & freq, ICRn
	// 010	Prescale 8
	TCCR3B=0x12;
	// Clear TCNT
	TCNT3H=0x00;
	TCNT3L=0x00;

	ICR3 = 20000;	// 20mS PWM cycle time
}

void pwmOffE345(void) {
	/**********************************************************/
	/* Return timer and pins to normal use on pins E3, E4, E5 */
	/**********************************************************/
	// 00	A off, toggle on compare
	// 00	B on, toggle on compare
	// 00	C on, toggle on compare
	// 00	Low part of WGM: Normal
	TCCR3A=0x00;
	// 0	No input noise cancelling
	// 0	Input edge select does not matter
	// 0	Not used
	// 00	High part of WGM: Normal
	// 000	Prescale None
	TCCR3B=0x00;
	// Clear TCNT
	TCNT3H=0x00;
	TCNT3L=0x00;

	ICR3 = 0;	// Clear ICR4
}
void pwmInitH345(void) {
	/***************************************************/
	/* Initialize timer for PWM use on pins H3, H4, H5 */
	/***************************************************/
	// 00	A off, toggle on compare
	// 00	B on, toggle on compare
	// 00	C on, toggle on compare
	// 00	Low part of WGM: PWM, phase & freq, ICRn
	TCCR4A=0x00;
	// 0	No input noise cancelling
	// 0	Input edge select does not matter
	// 0	Not used
	// 10	High part of WGM: PWM, phase & freq, ICRn
	// 010	Prescale 8
	TCCR4B=0x12;
	// Clear TCNT
	TCNT4H=0x00;
	TCNT4L=0x00;

	ICR4 = 20000;	// 20mS PWM cycle time
}

void pwmOffH345(void) {
	/**********************************************************/
	/* Return timer and pins to normal use on pins H3, H4, H5 */
	/**********************************************************/
	// 00	A off
	// 00	B off
	// 00	C off
	// 00	Low part of WGM: Normal
	TCCR4A=0x00;
	// 0	No input noise cancelling
	// 0	Input edge select does not matter
	// 0	Not used
	// 00	High part of WGM: Normal
	// 000	Prescale None
	TCCR4B=0x00;
	// Clear TCNT
	TCNT4H=0x00;
	TCNT4L=0x00;

	ICR4 = 0;	// Clear ICR4
}


void pwmOnE3(void) {
	/****************************/
	/* Put pin E3 into PWM mode */
	/****************************/
	// 10	A on, toggle on compare
	sbi(TCCR3A,7);
	cbi(TCCR3A,6);
}
void pwmOffE3(void) {
	/********************************/
	/* Return pin E3 to normal mode */
	/********************************/
	// 00	A off
	cbi(TCCR3A,7);
	cbi(TCCR3A,6);
}
void pwmOnE4(void) {
	/****************************/
	/* Put pin E4 into PWM mode */
	/****************************/
	// 10	B on, toggle on compare
	sbi(TCCR3A,5);
	cbi(TCCR3A,4);
}
void pwmOffE4(void) {
	/********************************/
	/* Return pin E4 to normal mode */
	/********************************/
	// 00	B off
	cbi(TCCR3A,5);
	cbi(TCCR3A,4);
}
void pwmOnE5(void) {
	/****************************/
	/* Put pin E5 into PWM mode */
	/****************************/
	// 10	C on, toggle on compare
	sbi(TCCR3A,3);
	cbi(TCCR3A,2);
}
void pwmOffE5(void) {
	/********************************/
	/* Return pin E5 to normal mode */
	/********************************/
	// 00	C off
	cbi(TCCR3A,3);
	cbi(TCCR3A,2);
}
void pwmOnH3(void) {
	/****************************/
	/* Put pin H3 into PWM mode */
	/****************************/
	// 10	A on, toggle on compare
	sbi(TCCR4A,7);
	cbi(TCCR4A,6);
}
void pwmOffH3(void) {
	/********************************/
	/* Return pin H3 to normal mode */
	/********************************/
	// 00	A off
	cbi(TCCR4A,7);
	cbi(TCCR4A,6);
}
void pwmOnH4(void) {
	/****************************/
	/* Put pin H4 into PWM mode */
	/****************************/
	// 10	B on, toggle on compare
	sbi(TCCR4A,5);
	cbi(TCCR4A,4);
}
void pwmOffH4(void) {
	/********************************/
	/* Return pin H4 to normal mode */
	/********************************/
	// 00	B off
	cbi(TCCR4A,5);
	cbi(TCCR4A,4);
}
void pwmOnH5(void) {
	/****************************/
	/* Put pin H5 into PWM mode */
	/****************************/
	// 10	C on, toggle on compare
	sbi(TCCR4A,3);
	cbi(TCCR4A,2);
}
void pwmOffH5(void) {
	/********************************/
	/* Return pin H5 to normal mode */
	/********************************/
	// 00	C off
	cbi(TCCR4A,3);
	cbi(TCCR4A,2);
}

/******************************/
/* Set pin pulse times in uS. */
/* Typical servo would use    */
/*     1500 for center        */
/*     1000 for full left     */
/*     2000 for full right    */
/******************************/
#define pwmSetE3(val)	OCR3A=val		// Set E3 pulse time in uS
#define pwmSetE4(val)	OCR3B=val		// Set E4 pulse time in uS
#define pwmSetE5(val)	OCR3C=val		// Set E5 pulse time in uS
#define pwmSetH3(val)	OCR4A=val		// Set H3 pulse time in uS
#define pwmSetH4(val)	OCR4B=val		// Set H4 pulse time in uS
#define pwmSetH5(val)	OCR4C=val		// Set H5 pulse time in uS



void pwmInit(void) {
	// Intialize the PWM module
#ifdef pwmShowInitStatus
	rprintf("      PWM:\t%d.%d\t",pwmVer,pwmRev);
	rprintf("done\n");;
#endif
}
